﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

namespace GMS.BIZ
{
    public class ClassInfo
    {
        #region Model
        private string classID;
        public string ClassID
        {
            get { return classID; }
            set { classID = value; }
        }
        private string classTeacher;
        public string ClassTeacher
        {
            get { return classTeacher; }
            set { classTeacher = value; }
        }
        private string classDescription;
        public string ClassDescription
        {
            get { return classDescription; }
            set { classDescription = value; }
        }
        #endregion Model

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="classID">班级ID</param>
        public ClassInfo(string classID)
        {
            this.classID = classID;

            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            GMS.ORM.Model.UClass myUClass = new GMS.ORM.Model.UClass();
            myUClass = optUClass.GetModel(classID);
            this.classTeacher = myUClass.class_teacher;
            this.classDescription = myUClass.class_desc;
        }

        /// <summary>
        /// 计算本班级中的学生数量
        /// </summary>
        public int CountStudents()
        {
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            DataSet ds = optGStudent.GetList("class_id='" + this.classID + "'");
            if (ds == null) return 0;
            return ds.Tables[0].Rows.Count;
        }

        /// <summary>
        /// 检查某个学生是否在这个班级中
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public bool HasStudent(string studentID)
        {
            //检查学生是否存在，不存在返回false
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            bool ok = optGStudent.Exists(studentID);
            if (!ok) return false;
            else
            {
                //检查学生是否在此班级中，不是的返回false
                GMS.ORM.Model.GStudent myGStudent = new GMS.ORM.Model.GStudent();
                myGStudent=optGStudent.GetModel(studentID);
                if (!(this.classID.CompareTo(myGStudent.class_id) == 0))
                    return false;
                else return true;
            }
        }

        /// <summary>
        /// 获得本班级中的一个学生
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Student GetOneStudent(string studentID)
        {
            //检查学生是否在班级中，不在班级中返回null
            GMS.ORM.Model.GStudent myGStudent = new GMS.ORM.Model.GStudent();
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            myGStudent = optGStudent.GetModel(studentID);
            if (!(this.classID.CompareTo(myGStudent.class_id) == 0))
                return null;
            //成功，初始化Student对象，并返回
            else
            {
                GMS.BIZ.Student myStudent = new Student(studentID);
                return myStudent;
            }
        }

        /// <summary>
        /// 获得本班级的学生列表
        /// </summary>
        public ArrayList GetStudentsList()
        {
            //将本班级的学生学号一个个add入arraylist
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            DataSet ds = optGStudent.GetList("class_id='" + this.classID + "'");
            if (ds == null) return null;
            ArrayList arraylist = new ArrayList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                arraylist.Add(ds.Tables[0].Rows[i]["std_id"].ToString());
            }
            return arraylist;
        }

        /// <summary>
        /// 获得本班级的学生Dataset
        /// </summary>
        /// <returns></returns>
        public DataSet GetStudentsDataSet()
        {
            //将本班级的学生Dataset返回，Dataset为DAL.GStudent.GetList事件所得到的Dataset
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            return optGStudent.GetList("class_id='" + this.classID + "' ORDER BY std_id");
        }

        /// <summary>
        /// 获得本班级的学生论文情况
        /// </summary>
        public DataSet GetStudentsDataSetWithPaperInfo()
        {
            //返回本班级学生的DataSet，Dataset的Table需要自己组织
                      
            //获取本班级的学生列表
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            DataSet ds = optGStudent.GetList("class_id='" + this.classID + "'");
            if (ds == null) return null;
            //创建学生论文情况列表
            DataSet ds1 = new DataSet();
            DataTable dt = new DataTable();
            ds1.Tables.Add(dt);
            dt.Columns.Add("学生学号");
            dt.Columns.Add("学生姓名");
            dt.Columns.Add("立题申报");
            dt.Columns.Add("任务书");
            dt.Columns.Add("开题报告");
            dt.Columns.Add("论文");
            dt.Columns.Add("作品");
            dt.Columns.Add("翻译");    
            
            
            
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["学生学号"] = ds.Tables[0].Rows[i]["std_id"];
                dr["学生姓名"] = ds.Tables[0].Rows[i]["std_name"];
                GMS.ORM.DAL.PGuide optPGuide = new GMS.ORM.DAL.PGuide();
                GMS.ORM.Model.PGuide myPGuide = new GMS.ORM.Model.PGuide();
                myPGuide = optPGuide.GetModel((ds.Tables[0].Rows[i]["std_id"]).ToString(), (ds.Tables[0].Rows[i]["graduate_year"]).ToString());
                if (myPGuide != null)
                {
                    dr["立题申报"] = myPGuide.doc1;
                    dr["任务书"] = myPGuide.doc2;
                    dr["开题报告"] = myPGuide.doc3;
                    dr["论文"] = myPGuide.doc4;
                    dr["作品"] = myPGuide.doc5;
                    dr["翻译"] = myPGuide.doc6;
                    dt.Rows.Add(dr);
                }
            }

           
                //Table中的列为：studentID,studentName,doc1,doc2,doc3,....doc6（6个文档的状态的中文）
            return ds1;
        }

        /// <summary>
        /// 获得本班级的学生答辩信息
        /// </summary>
        public DataSet GetStudentsDataSetWithDefenseInfo()
        {
            //返回本班级学生的DataSet，Dataset的Table需要自己组织
            //获取本班级的学生列表
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            DataSet ds = optGStudent.GetList("class_id='" + this.classID + "'");

            if (ds == null) return null;
                        
            //创建学生答辩情况列表
            DataSet ds1 = new DataSet();
            DataTable dt = new DataTable();
            ds1.Tables.Add(dt);
            dt.Columns.Add("studentID");
            dt.Columns.Add("studentName");
            dt.Columns.Add("defenseLibID");
            dt.Columns.Add("defenseTime");
            dt.Columns.Add("defenseRoom");
            dt.Columns.Add("def_teacher");
            dt.Columns.Add("def_assistant");
            //Table中的列为：studentID,studentName,defenseLibID,defenseTime,defenseRoom,def_teacher(将5个字段def_tch_1到def_tch_5合并，教师名与教师名之间用空格分开),def_assistant
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["studentID"] = ds.Tables[0].Rows[i]["std_id"];
                dr["studentName"] = ds.Tables[0].Rows[i]["std_name"]; 
                DataSet ds2=GMS.BIZ.Defense.GetDefenseList(ds.Tables[0].Rows[i]["std_id"].ToString(), ds.Tables[0].Rows[i]["graduate_year"].ToString());
                if (ds2 != null)
                {
                    dr["defenseLibID"] = ds2.Tables[0].Rows[0]["def_id"];
                    dr["defenseTime"] = ds2.Tables[0].Rows[0]["def_time"];
                    dr["defenseRoom"] = ds2.Tables[0].Rows[0]["def_room"];
                    dr["def_teacher"] = ds2.Tables[0].Rows[0]["def_tch_1"] + " " + ds2.Tables[0].Rows[0]["def_tch_2"] + " " + ds2.Tables[0].Rows[0]["def_tch_3"] + " " + ds2.Tables[0].Rows[0]["def_tch_4"] + " " + ds2.Tables[0].Rows[0]["def_tch_5"];
                    dr["def_assistant"] = ds2.Tables[0].Rows[0]["def_assistant"];
                    dt.Rows.Add(dr);
                }
            }
            return ds1;
        }
    }
}
